// Copyright 2008 mateamargo

//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at

//        http://www.apache.org/licenses/LICENSE-2.0

//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

package btb.core.sprites;

import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

import btb.core.Direction;
import btb.core.BtbGame;
import btb.core.Status;

import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.util.ImageUtil;

/**
 * The Batman's Sprite specialization process its collisions
 * and interacts with the user inputs
 * @author mateamargo
 *
 */
public class BatmanSprite extends Sprite {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -627879271099120895L;
	
	private Status status = Status.STANDING;
	
	private Map<Status, BufferedImage> statusImages = new HashMap<Status, BufferedImage>();
	
	private Direction direction = Direction.LEFT;
	
	private double step = 0.2;
	
	private double kickTime = 200; // A half second
	
	private double elapsedSum = 0;
	
	private double energy = 500;
	
	private BtbGame game;
	
	private String imgStanding = "/home/mateamargo/Projects/Games/beat-the-beatles2/beat-the-beatles-core/src/main/resources/batman-walking.gif";
	private String imgMoving = "/home/mateamargo/Projects/Games/beat-the-beatles2/beat-the-beatles-core/src/main/resources/batman-walking.gif";
	private String imgKicking = "/home/mateamargo/Projects/Games/beat-the-beatles2/beat-the-beatles-core/src/main/resources/batman-kicking.png";
	private String imgHitting = "/home/mateamargo/Projects/Games/beat-the-beatles2/beat-the-beatles-core/src/main/resources/batman-walking.gif";

	public BatmanSprite(BtbGame game, double x, double y) {
		super(x, y);
		
		this.game = game;
		
		statusImages.put(Status.STANDING, game.getImage(imgStanding));
		statusImages.put(Status.MOVING, game.getImage(imgMoving));
		statusImages.put(Status.KICKING, game.getImage(imgKicking));
		statusImages.put(Status.HITTING, game.getImage(imgHitting));
		
		setImage(statusImages.get(Status.STANDING));
	}

	private void changeStatus(Status status){
		this.status = status;
		setImage(statusImages.get(status));
	}
	
	public void doLogic(long elapsedTime){
		elapsedSum += elapsedTime;
		
		if(status == Status.KICKING && elapsedSum <= kickTime){
			return;
		}else{
			elapsedSum = 0;
		}
		
		if(isMoving(elapsedTime)){
			changeStatus(Status.MOVING);
		}
		
		if(game.keyPressed(KeyEvent.VK_SPACE)){
			changeStatus(Status.KICKING);
		}else{
			changeStatus(Status.STANDING);
		}
	}
	
	private boolean isMoving(long elapsedTime){
		if(game.keyDown(KeyEvent.VK_DOWN) || game.keyDown(KeyEvent.VK_UP) || 
				game.keyDown(KeyEvent.VK_LEFT) || 	game.keyDown(KeyEvent.VK_RIGHT)){
			
			if (game.keyDown(KeyEvent.VK_DOWN) && getY() < game.getBottomRange()) {
				move(0, step * elapsedTime);
			}
			
			if(game.keyDown(KeyEvent.VK_UP) && getY() > game.getTopRange()){
				move(0, -step * elapsedTime);
			}
			
			if(game.keyDown(KeyEvent.VK_LEFT) && getX() > game.getLeftRange()){
				move(-step * elapsedTime, 0);
				
				if(direction != Direction.LEFT){
					direction = Direction.LEFT;
					setImage(ImageUtil.flip(getImage()));
//					setImage(ImageUtil.flipHorizontal(getImage()));
				}
			}
			
			if(game.keyDown(KeyEvent.VK_RIGHT) && getX() < game.getRightRange()){
				move(step * elapsedTime, 0);
				
				if(direction != Direction.RIGTH){
					direction = Direction.RIGTH;
					setImage(ImageUtil.flip(getImage()));
//					setImage(ImageUtil.flipHorizontal(getImage()));
				}
			}
			
			return true;
			
		}else{
			return false;
		}
	}
	
	public void collided(Sprite beatle){
		if(status == Status.KICKING){
			game.getBeatles().remove(beatle);
			beatle.setActive(false);
		}else{
			energy -= 1;
		}
		
		System.out.println("Energy left!: " + energy);
		
		if(energy <= 0){
			game.notifyDeath();
		}
	}

}
